import type { Module } from 'vuex'

import { IDashBoard } from './type'
import { IRootState } from '../../type'

import {
  getCategoryGoodsCount,
  getCategoryGoodsSale,
  getCategoryGoodsFavor,
  getAddressGoodsSale
} from '@/network/main/analysis/dashboard'

const dashBoardModule: Module<IDashBoard, IRootState> = {
  // 需要加上namespaced，否则dispatch/commit提交vuex识别不成功
  namespaced: true,
  state() {
    return {
      categoryGoodsCount: [],
      categoryGoodsSale: [],
      categoryGoodsFavor: [],
      addressGoodsSale: []
    }
  },
  mutations: {
    changeGoodsCount(state, list) {
      state.categoryGoodsCount = list
    },
    changeGoodsSale(state, list) {
      state.categoryGoodsSale = list
    },
    changeGoodsFavor(state, list) {
      state.categoryGoodsFavor = list
    },
    changeGoodsAddress(state, list) {
      state.addressGoodsSale = list
    }
  },
  actions: {
    // dashboard页面数据
    async getDashboardAction({ commit }) {
      const countResult = await getCategoryGoodsCount()
      commit('changeGoodsCount', countResult.data)
      const saleResult = await getCategoryGoodsSale()
      commit('changeGoodsSale', saleResult.data)
      const favorResult = await getCategoryGoodsFavor()
      commit('changeGoodsFavor', favorResult.data)
      const addressResult = await getAddressGoodsSale()
      commit('changeGoodsAddress', addressResult.data)
      console.log(countResult, saleResult, favorResult, addressResult)
    }
  }
}

export default dashBoardModule
